package com.collect.tanzania.zhiScene.zhiWidget.zhiDialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.collect.tanzania.R;
import com.collect.tanzania.zhiUtils.StringUtil;

public class ZHIDialogNumberInput extends Dialog {

    private final Context mContext;

    private TextView tv_title, btn_negtive;

    private EditText et_input;

    private String mTitle;

    private String mContent;

    private String mHintText;

    private ZHIDialogItemClickListener mZHIDialogItemClickListener;

    public static void showDialog(Context context, String title, String content, String hintText, ZHIDialogItemClickListener listener) {
        if (context == null) {
            return;
        }
        ZHIDialogNumberInput zhiDialogNumberInput = new ZHIDialogNumberInput(context);
        zhiDialogNumberInput.mTitle = StringUtil.safeString(title);
        zhiDialogNumberInput.mContent = StringUtil.safeString(content);
        zhiDialogNumberInput.mHintText = StringUtil.safeString(hintText);
        zhiDialogNumberInput.mZHIDialogItemClickListener = listener;
        zhiDialogNumberInput.show();
    }

    public ZHIDialogNumberInput(Context context) {
        super(context, R.style.CustomDialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_number_input);
        initView();
    }

    @Override
    public void show() {
        super.show();
        refreshViewData();
    }

    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        et_input = (EditText) findViewById(R.id.et_input);
        btn_negtive = (TextView) findViewById(R.id.btn_negtive);

        btn_negtive.setOnClickListener(v -> {
            if (mZHIDialogItemClickListener != null) {
                mZHIDialogItemClickListener.onConfirmClick(StringUtil.safeString(et_input.getText().toString().trim()));
            }
            dismiss();
        });
    }

    private void refreshViewData() {
        if (!TextUtils.isEmpty(mTitle)) {
            tv_title.setText(mTitle);
        }
        if (!TextUtils.isEmpty(mHintText)) {
            et_input.setHint(mHintText);
        }
        if (!TextUtils.isEmpty(mContent)) {
            et_input.setText(mContent);
        }
    }

}
